<template>
  <div class="login-container">
    <div class="login-form">
      <h2>Login</h2>
      <div class="form-group">
        手 机 号：
        <input type="text" id="phone" v-model="userInfo.phone" placeholder="请输入手机号" style=" margin-left: 8px;">
      </div>
      <div class="form-group">
        <span style=" margin-left: 90px;">验 证 码：</span>
        <input type="text" id="code" v-model="userInfo.code" placeholder="请输入验证码" style="margin-left: 10px">
        <button class="button" style="width: 70px; height: 30px" :disabled="flag" @click="sendCode">{{str}}</button>
      </div>
      <div class="form-group">
        <button class="butt" @click="login">登录</button>
      </div>
    </div>
  </div>
</template>

<script>
let url = 'http://localhost:9701/web';
export default {
  data() {
    return {
      userInfo:{
        code: '',
        phone: '',
      },
      flag: false,
      str: '获取验证码',
      countDown: 60
    };
  },
  watch : {
    flag() {
      if (this.flag) {
        this.str = "已发送(" + this.countDown + "s)";
        let timer = setInterval(() => {
          this.countDown--;
          if (this.countDown > 0) {
            this.str = "已发送(" + this.countDown + "s)";
          } else {
            this.str = "获取验证码";
            this.countDown = 60;
            this.flag = false;
            clearInterval(timer);
          }
        }, 1000, 60)
      }
    }
  },
  methods: {

    /**
     * 登录
     */
    login() {
      if (this.userInfo.phone === '') {
        this.$message.error('手机号不能为空')
        return
      } else {
        // 手机号格式验证
        const phoneReg = /^1[3-9]\d{9}$/
        if (!phoneReg.test(this.userInfo.phone)) {
          this.$message.warning('请输入正确的手机号格式')
          return
        }
      }
      if (this.userInfo.code === null || this.userInfo.code === '' || this.userInfo.code.length !== 4) {
        this.$message.error('请输入正确的验证码')
        return
      }
      this.axios.post(url + '/userInfo/login', this.userInfo).then(res => {
        if (res.data.code === 200) {
          this.$message({
            message: '登录成功',
            type: 'success'
          });
          localStorage.setItem('token', res.data.data)
          this.$router.push('/house')
        }
      })
    },
    /**
     * 获取验证码
     */
    sendCode() {
      if (this.userInfo.phone === '') {
        this.$message.error('手机号不能为空')
        return
      } else {
        // 手机号格式验证
        const phoneReg = /^1[3-9]\d{9}$/
        if (!phoneReg.test(this.userInfo.phone)) {
          this.$message.warning('请输入正确的手机号格式')
          return
        }
      }
      this.axios.post(url + '/userInfo/sendCode', this.userInfo).then(res => {
        if (res.data.code === 200) {
          this.$message({
            message: '验证码发送成功,验证码为：'+res.data.data,
            type: 'success'
          });
          this.flag = true;
        } else {
          this.$message({
            message: '验证码发送失败',
            type: 'error'
          });
        }
      })
    }
  }
};
</script>

<style scoped>
.login-container {
  background-image: url('@/assets/bj.jpg ');
  background-size: cover;
  background-position: center;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-form {
  padding: 100px;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}

.form-group {
  margin-bottom: 15px;
}

.form-group label {
  display: block;
  margin-bottom: 5px;
}

.form-group input {
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 5px;
}

h2 {
  margin-bottom: 60px;
  font-size: 30px;
}


.form-group .button {
  display: block;
  width: 100px;
  padding: 6px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  color: #151414;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 10px;
  transition: background-color 0.3s ease;
  float: right;
  margin-left: 20px;
}

.butt {
  margin-top: 20px;
  width: 150px;
  height: 40px;
  background-color: #faff9c;
  border: none;
  margin-left: 50px;
  border-radius: 5px;
}
</style>